﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace Kevin.SLUserControlLibrary
{
    public class IMETextBox : TextBox
    {
        private bool inOnShift = false;
        private string textBak = string.Empty;

        public enum ImeStateType
        {
            None,
            HankakuAll,
            HankakuNum,
            Zenkaku,
        }

        public static DependencyProperty IMEStateProperty = DependencyProperty.Register("IMEState",
           typeof(ImeStateType), typeof(IMETextBox), new PropertyMetadata(ImeStateType.None, new PropertyChangedCallback(OnImeStatePropertyChanged)));

        public IMETextBox()
        {
            this.KeyDown += new KeyEventHandler(MaskedTextBox_KeyDown);
            this.KeyUp += new KeyEventHandler(MaskedTextBox_KeyUp);
            this.TextChanged += new TextChangedEventHandler(IMETextBox_TextChanged);

        }

        private void IMETextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.IMEState == ImeStateType.HankakuNum)
            {
                if (this.IsHankaku(this.Text) && this.IsNum(this.Text))
                {
                    this.textBak = this.Text;
                }
                else
                {
                    this.Text = this.textBak;
                }
            }
            else if (this.IMEState == ImeStateType.HankakuAll)
            {
                if (this.IsHankaku(this.Text))
                {
                    this.textBak = this.Text;
                }
                else
                {
                    this.Text = this.textBak;
                }
            }
            else if (this.IMEState == ImeStateType.Zenkaku)
            {
                if (this.IsZankaku(this.Text))
                {
                    this.textBak = this.Text;
                }
                else
                {
                    this.Text = this.textBak;
                }
            }
        }

        private bool IsHankaku(string targetString)
        {
            if (string.IsNullOrEmpty(targetString))
            {
                return true;
            }

            string reg = @"^[\u0020-\u007e\uff61-\uffef]+$";

            if (Regex.IsMatch(targetString, reg))
            {
                return true;
            }

            return false;
        }

        private bool IsZankaku(string targetString)
        {
            if (string.IsNullOrEmpty(targetString))
            {
                return true;
            }

            string reg = @"^[^\u0020-\u007e\uff61-\uffef]+$";

            if (Regex.IsMatch(targetString, reg))
            {
                return true;
            }
            return false;
        }

        private bool IsNum(string targetString)
        {
            if (string.IsNullOrEmpty(targetString))
            {
                return true;
            }

            string reg = @"^[0-9]+$";

            if (Regex.IsMatch(targetString, reg))
            {
                return true;
            }
            return false;
        }

        void MaskedTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Shift)
            {
                inOnShift = false;
            }
        }

        void MaskedTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Shift)
            {
                inOnShift = true;
            }

            int length = this.Text.Length;
            int index = length;

            if (this.IMEState == ImeStateType.HankakuNum)
            {
                if (!((inOnShift == false) && ((int)e.Key >= 20 && (int)e.Key <= 29) || ((int)e.Key == 2) || ((int)e.Key >= 68 && (int)e.Key <= 77)) && e.Key != Key.Enter)
                {
                    e.Handled = true;
                }
            }
        }

        public ImeStateType IMEState
        {
            get { return (ImeStateType)GetValue(IMEStateProperty); }
            set
            {
                SetValue(IMEStateProperty, value);
            }
        }

        private static void OnImeStatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if ((ImeStateType)e.NewValue == ImeStateType.None)
            {
                InputMethod.SetPreferredImeState(d, InputMethodState.DoNotCare);
                InputMethod.SetPreferredImeConversionMode(d, ImeConversionModeValues.NoConversion);
                InputMethod.SetIsInputMethodEnabled(d, true);
            }
            else if ((ImeStateType)e.NewValue == ImeStateType.HankakuAll || (ImeStateType)e.NewValue == ImeStateType.HankakuNum)
            {
                InputMethod.SetPreferredImeState(d, InputMethodState.On);
                InputMethod.SetPreferredImeConversionMode(d, ImeConversionModeValues.Alphanumeric);
                InputMethod.SetIsInputMethodEnabled(d, false);
            }
            //else if ((ImeStateType)e.NewValue == ImeStateType.Zenkaku)
            //{
            //    InputMethod.SetPreferredImeState(d, InputMethodState.On);
            //    InputMethod.SetPreferredImeConversionMode(d, ImeConversionModeValues.FullShape | ImeConversionModeValues.Native);
            //}
        }
    }
}
